//
//  VideoDetailTableViewCell.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/19.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "VideoDetailTableViewCell.h"
#import "AutoadaptationFrame.h"
#import "CustomerAVPlayer.h"
#import "AutoadaptationFrame.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "VideoCollectModel.h"
#import "CoreData.h"

@interface VideoDetailTableViewCell()

@property(nonatomic, strong) CoreData *coreDataManager;

//@property(nonatomic, retain) UIImageView *videoPic;

@property(nonatomic, retain) UILabel *timeLable;

@property(nonatomic, retain) UILabel *hotLabel;

//@property(nonatomic, retain) CustomerAVPlayer *avplayer;

@property(nonatomic, retain) UILabel *nameLabel;

@property(nonatomic, retain) UILabel *discussLabel;

@property(nonatomic, retain) UIButton *likePicButton;

@property(nonatomic, retain) UIButton *likeWordButton;

@property(nonatomic, retain) UIView *seperatorLine;

@property(nonatomic, retain) UIButton *forwardPicButton;

@property(nonatomic, retain) UIButton *forwardWordButton;

@property(nonatomic, retain) NSArray *resultArray;

@end

@implementation VideoDetailTableViewCell

- (void)dealloc
{
    //[_videoPic release];
    [_timeLable release];
    [_hotLabel release];
    //[_avplayer release];
    [_nameLabel release];
    [_discussLabel release];
    [_likePicButton release];
    [_likeWordButton release];
    [_seperatorLine release];
    [_forwardPicButton release];
    [_forwardWordButton release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.coreDataManager = [CoreData shareManagerWithFile:@"joke"];
        [self createView];
    }
    return self;
}

- (void)createView
{
//    self.videoPic = [[UIImageView alloc] init];
//    self.videoPic.userInteractionEnabled = YES;
//    [self.contentView addSubview:self.videoPic];
//    [_videoPic release];
    
    self.timeLable = [[UILabel alloc] init];
    self.timeLable.font = [UIFont systemFontOfSize:15];
    self.timeLable.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.timeLable];
    [_timeLable release];
    
    self.hotLabel = [[UILabel alloc] init];
    self.hotLabel.font = [UIFont systemFontOfSize:15];
    self.hotLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.hotLabel];
    [_hotLabel release];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.numberOfLines = 0;
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.discussLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_discussLabel];
    [_discussLabel release];
    
    self.likePicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_likePicButton addTarget:self action:@selector(collectedAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_likePicButton];
    
    self.likeWordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_likeWordButton setTitle:@"喜欢" forState:UIControlStateNormal];
    _likeWordButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_likeWordButton setTitleColor:[UIColor colorWithRed:0.5807 green:0.5807 blue:0.5807 alpha:1.0] forState:UIControlStateNormal];
    [_likeWordButton addTarget:self action:@selector(collectedAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_likeWordButton];
    
    self.seperatorLine = [[UIView alloc] init];
    _seperatorLine.backgroundColor = [UIColor colorWithRed:0.5807 green:0.5807 blue:0.5807 alpha:1.0];
    [self.contentView addSubview:_seperatorLine];
    [_seperatorLine release];
    
    self.forwardPicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_forwardPicButton setImage:[UIImage imageNamed:@"iconfont-share"] forState:UIControlStateNormal];
    [_forwardPicButton addTarget:self action:@selector(forwardAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_forwardPicButton];
    
    self.forwardWordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_forwardWordButton setTitle:@"转发" forState:UIControlStateNormal];
    _forwardWordButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_forwardWordButton setTitleColor:[UIColor colorWithRed:0.5807 green:0.5807 blue:0.5807 alpha:1.0] forState:UIControlStateNormal];
    [_forwardWordButton addTarget:self action:@selector(forwardAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_forwardWordButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //self.videoPic.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);
    self.nameLabel.frame = CGRectMake(10, 30, self.frame.size.width - 20, self.frame.size.height - 70);
    _discussLabel.frame = CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 5, [AutoadaptationFrame widthOfLabel:[NSString stringWithFormat:@"评论 %ld", self.discussNumber] andTextFont:[UIFont systemFontOfSize:15] andLableHeight:30], 30);
    _likePicButton.frame = CGRectMake(self.frame.size.width - 150, _discussLabel.frame.origin.y + 3, 20, 20);
#pragma mark 从coredata中读取数据
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoCollectModel" inManagedObjectContext:_coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [_coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"error: %@", error);
    }else
    {
        for (VideoCollectModel *videoCollectModel in fetchedObjects) {
            if ([videoCollectModel.scid isEqualToString:self.channel.scid]) {
                [_likePicButton setImage:[UIImage imageNamed:@"iconfont-heartfull"] forState:UIControlStateNormal];
            }else
            {
                [_likePicButton setImage:[UIImage imageNamed:@"iconfont-heartempty"] forState:UIControlStateNormal];
            }
        }
        if (fetchedObjects.count == 0) {
            [_likePicButton setImage:[UIImage imageNamed:@"iconfont-heartempty"] forState:UIControlStateNormal];
        }
    }
    
    _likeWordButton.frame = CGRectMake(_likePicButton.frame.origin.x + _likePicButton.frame.size.width, _likePicButton.frame.origin.y, 50, 20);
    _seperatorLine.frame = CGRectMake(_likeWordButton.frame.origin.x + _likeWordButton.frame.size.width + 3, _likeWordButton.frame.origin.y - 2, 0.5, 24);
    _forwardPicButton.frame = CGRectMake(_likeWordButton.frame.origin.x + _likeWordButton.frame.size.width + 10, _likeWordButton.frame.origin.y, 20, 20);
    
    _forwardWordButton.frame = CGRectMake(_forwardPicButton.frame.origin.x + _forwardPicButton.frame.size.width, _forwardPicButton.frame.origin.y, 50, 20);
}

- (void)setChannel:(Channel *)channel
{
    if (_channel != channel) {
        [_channel release];
        _channel = [channel retain];
    }
//    NSString *picUrl = [NSString stringWithFormat:@"%@%@", channel.pic.base, channel.pic.m];
    //[self.videoPic sd_setImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    self.timeLable.frame = CGRectMake(10, 0, [AutoadaptationFrame widthOfLabel:channel.ext2.createTimeNice andTextFont:[UIFont systemFontOfSize:15] andLableHeight:30], 30);
    self.timeLable.text = channel.ext2.createTimeNice;
    
    if (channel != nil) {
        self.hotLabel.frame = CGRectMake(20 + [AutoadaptationFrame widthOfLabel:channel.ext2.createTimeNice andTextFont:[UIFont systemFontOfSize:15] andLableHeight:30], 0, 100, 30);
        self.hotLabel.textColor = [UIColor grayColor];
        NSString *hotStr = [NSString stringWithFormat:@"  %@热度", channel.stat.vcntNice];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:hotStr];
        NSRange range = [hotStr rangeOfString:channel.stat.vcntNice];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [self.hotLabel setAttributedText:attrStr];
        
//        self.avplayer = [CustomerAVPlayer shareCustomerAVPlayer];
//        self.avplayer.frame = CGRectMake(0, 0, self.videoPic.frame.size.width, self.videoPic.frame.size.height);
//        self.avplayer.playURL = [NSString stringWithFormat:@"%@%@.%@", channel.stream.base, channel.scid, channel.stream.and];
//        [self.videoPic addSubview:self.avplayer];
    }
    self.nameLabel.text = channel.ext.t;
}

- (void)setDiscussNumber:(NSInteger)discussNumber
{
    if (_discussNumber != discussNumber) {
        _discussNumber = discussNumber;
    }
    _discussLabel.text = [NSString stringWithFormat:@"评论 %ld", self.discussNumber];
    _discussLabel.font = [UIFont systemFontOfSize:15];
    _discussLabel.textColor = [UIColor colorWithRed:0.5807 green:0.5807 blue:0.5807 alpha:1.0];

}

- (void)collectedAction
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoCollectModel" inManagedObjectContext:_coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scid"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [_coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.resultArray = fetchedObjects;
    if ([_likePicButton.imageView.image isEqual:[UIImage imageNamed:@"iconfont-heartempty"]]) {
        VideoCollectModel *videoCollectModel = [NSEntityDescription insertNewObjectForEntityForName:@"VideoCollectModel" inManagedObjectContext:_coreDataManager.managedObjectContext];
        videoCollectModel.posterPic = [NSString stringWithFormat:@"%@%@", self.channel.pic.base, self.channel.pic.m];
        videoCollectModel.describe = self.channel.ext.t;
        videoCollectModel.scid = self.channel.scid;
        [_coreDataManager saveContext];
        [_likePicButton setImage:[UIImage imageNamed:@"iconfont-heartfull"] forState:UIControlStateNormal];

    }else
    {
        for (VideoCollectModel *videoCollectModel in self.resultArray) {
            if ([videoCollectModel.scid isEqualToString:self.channel.scid]) {
                [_coreDataManager.managedObjectContext deleteObject:videoCollectModel];
            }
        }
        [_coreDataManager saveContext];
        [_likePicButton setImage:[UIImage imageNamed:@"iconfont-heartempty"] forState:UIControlStateNormal];
    }
    
}

- (void)forwardAction
{
    [self.delegate shareButtonDelegateAction];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
